import { toFixed, dateFmt, getModelValByKey, getOssImg2, getDistanceTxt, getDateDesc, getDateFormat, percent, dateDescfrt } from '@/utils'

const filters = {
  // 没值时的提示
  tip(v, tip) {
    if (!v && v !== 0) {
      return tip
    }
    return v
  },

  // 字符串拼接
  join(v, str, position = 'right') {
    if (position === 'right') {
      return v + '' + str
    } else if (position === 'left') {
      return str + '' + v
    }
  },

  // 默认图片头像
  defaultSrc(v, src) {
    if (v) {
      return v
    }
    return src || getOssImg2('my/defaultTX.png')
  },

  // 分割字符
  slice(v, len, index = 0) {
    const str = (v + '').slice(index, len)
    return str
  },

  maxlen(v, len) {
    v = v + ''
    const vLen = v.length
    if (vLen > len) {
      return v.slice(0, len) + '...'
    }
    return v
  },

  purchaseQuantity(v) {
    if (v === '0' || !v) {
      return '不限购'
    } else {
      return `每人限购${v}件`
    }
  },
  // 数组转字符串
  array2Str(v, sign = ',') {
    if (v instanceof Array) {
      return v.join(sign)
    }
    return v
  },

  // filter model 数据里面的值
  toFixed,
  dateFmt,
  getModelValByKey,
  getDistanceTxt,
  getDateDesc,
  getDateFormat,
  dateDescfrt,
  percent
}
export default (Vue) => {
  Object.keys(filters).forEach((key) => {
    Vue.filter(key, filters[key])
  })
}
// export default filters
